﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using OneoneNine.Data;
using OneoneNine.Models;

namespace OneoneNine.Controllers
{
    [Authorize]
    public class ExaminesController : Controller
    {
        private readonly ApplicationDbContext _context;

        public ExaminesController(ApplicationDbContext context)
        {
            _context = context;
        }

        // GET: Examines
        public async Task<IActionResult> Index(string name)
        {
            var examineList = await _context.Examine
                   .Where(e => e.StudentName == name) // 使用 name 进行筛选
                   .ToListAsync();

            return View(examineList);
        }
        public async Task<IActionResult> ExamineView()
        {
            return View(await _context.Examine.ToListAsync());
        }

        // GET: Examines/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var examine = await _context.Examine
                .FirstOrDefaultAsync(m => m.Id == id);
            if (examine == null)
            {
                return NotFound();
            }

            return View(examine);
        }

        // GET: Examines/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: Examines/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,StudentName,StartDate,EndDate,Reason,State")] Examine examine)
        {
            if (ModelState.IsValid)
            {
                _context.Add(examine);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index), new { name = examine.StudentName });
            }
            return View(examine);
        }
        // GET: Examines/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var examine = await _context.Examine.FindAsync(id);
            if (examine == null)
            {
                return NotFound();
            }
            return View(examine);
        }
        // POST: Examines/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,StudentName,StartDate,EndDate,Reason,State")] Examine examine)
        {
            if (id != examine.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(examine);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ExamineExists(examine.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(examine);
        }
        // GET: Examines/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var examine = await _context.Examine
                .FirstOrDefaultAsync(m => m.Id == id);
            if (examine == null)
            {
                return NotFound();
            }

            return View(examine);
        }

        // POST: Examines/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var examine = await _context.Examine.FindAsync(id);
            _context.Examine.Remove(examine);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool ExamineExists(int id)
        {
            return _context.Examine.Any(e => e.Id == id);
        }
    }
}
